package org.jeecg.modules.rooms.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.business.entity.BusBookingRooms;
import org.jeecg.modules.business.entity.BusHotel;
import org.jeecg.modules.business.entity.BusRoomBookingOrders;
import org.jeecg.modules.business.enums.CheckInTypeEnum;
import org.jeecg.modules.business.mapper.BusRoomBookingOrdersMapper;
import org.jeecg.modules.business.service.IBusBookingRoomsService;
import org.jeecg.modules.business.service.impl.BusHotelServiceImpl;
import org.jeecg.modules.rooms.DTO.*;
import org.jeecg.modules.rooms.Enum.CouponEnums;
import org.jeecg.modules.rooms.Enum.RoomStatusEnum;
import org.jeecg.modules.rooms.Vo.CanUseBuildingRoomsVo;
import org.jeecg.modules.rooms.Vo.CanUseResultVo;
import org.jeecg.modules.rooms.Vo.CesRoomsVo;
import org.jeecg.modules.rooms.Vo.LivingSourceStatVo;
import org.jeecg.modules.rooms.entity.CesHourRoomRule;
import org.jeecg.modules.rooms.entity.CesRoomBuildingFloor;
import org.jeecg.modules.rooms.entity.CesRoomLayout;
import org.jeecg.modules.rooms.entity.CesRooms;
import org.jeecg.modules.rooms.mapper.CesRoomsMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fendo
 * @since 2023-03-07
 */
@Service
public class CesRoomsServiceImpl extends ServiceImpl<CesRoomsMapper, CesRooms> implements IService<CesRooms> {

    @Resource
    private CesRoomsMapper roomsMapper;

    @Resource
    private BusHotelServiceImpl busHotelService;

    @Resource
    private CesRoomBuildingFloorServiceImpl buildingFloorService;

    @Resource
    private CesRoomLayoutServiceImpl layoutService;

    @Resource
    private CesAllDayPriceRuleServiceImpl allDayPriceRuleService;

    @Resource
    private CesHourRoomRuleServiceImpl hourRoomRuleService;

    @Resource
    private IBusBookingRoomsService bookingRoomsService;

    @Resource
    private BusRoomBookingOrdersMapper roomBookingOrdersMapper;

    /**
     * 查询房间
     *
     * @param dto
     * @return
     */
    public Result list(CesRoomSearchDto dto) {
        QueryWrapper<CesRooms> queryWrapper = new QueryWrapper<CesRooms>()
                .eq(!StringUtils.isBlank(dto.getBuildId()),CesRooms.BUILD_ID, dto.getBuildId())
                .eq(!StringUtils.isBlank(dto.getFloorId()),CesRooms.FLOOR_ID, dto.getFloorId())
                .eq(!StringUtils.isBlank(dto.getHotelId()),CesRooms.HOTEL_ID, dto.getHotelId())
                .like(!StringUtils.isBlank(dto.getRoomName()),CesRooms.NAME, dto.getRoomName())
                .eq(CesRooms.INVALID, false).orderByDesc(CesRooms.SORT, CesRooms.CREATEAT);
        IPage<CesRooms> dataPage = roomsMapper.selectPage(new Page<>(dto.getPageNo().intValue(), dto.getPageSize().intValue()), queryWrapper);
        List<CesRooms> records = dataPage.getRecords();
        Map<String, Object> map = new HashedMap();

        if (!CollectionUtil.isEmpty(records)) {

            List<String> hotelIds = records.stream().map(v -> v.getHotelId()).collect(Collectors.toList());
            List<String> buildIds = records.stream().map(v -> v.getBuildId()).collect(Collectors.toList());
            List<String> floorIds = records.stream().map(v -> v.getFloorId()).collect(Collectors.toList());

            List<BusHotel> busHotels = busHotelService.findHotelsByIds(hotelIds);
            buildIds.addAll(floorIds);
            List<CesRoomBuildingFloor> buildingFloors = buildingFloorService.getBuildFloorsByIds(buildIds);

            List<CesRoomsVo> result = new ArrayList<>();

            records.forEach(v -> {
                CesRoomsVo vo = new CesRoomsVo();
                BeanUtil.copyProperties(v, vo);

                Optional<BusHotel> hotelOptional = busHotels.stream().filter(h -> h.getId().equals(v.getHotelId())).findFirst();
                if (hotelOptional.isPresent()) vo.setHotelName(hotelOptional.get().getName());

                Optional<CesRoomBuildingFloor> buildingOptional = buildingFloors.stream().filter(b -> b.getId().equals(v.getBuildId())).findFirst();
                if (buildingOptional.isPresent()) vo.setBuildName(buildingOptional.get().getName());

                Optional<CesRoomBuildingFloor> floorOptional = buildingFloors.stream().filter(b -> b.getId().equals(v.getFloorId())).findFirst();
                if (floorOptional.isPresent()) vo.setFloorName(floorOptional.get().getName());

                result.add(vo);
            });
            map.put("records", result);
        } else {
            map.put("records", records);
        }
        map.put("total", dataPage.getTotal());
        return Result.OK(map);
    }

    /**
     * 修改房间密码
     *
     * @param dto
     * @return
     */
    public Boolean batchEditPassword(CesRoomSearchDto dto, String type, String firstValue, String endValue) {
        QueryWrapper<CesRooms> queryWrapper = new QueryWrapper<CesRooms>()
                .eq(!StringUtils.isBlank(dto.getBuildId()),CesRooms.BUILD_ID, dto.getBuildId())
                .eq(!StringUtils.isBlank(dto.getFloorId()),CesRooms.FLOOR_ID, dto.getFloorId())
                .eq(!StringUtils.isBlank(dto.getHotelId()),CesRooms.HOTEL_ID, dto.getHotelId())
                .eq(CesRooms.INVALID, false);
        List<CesRooms> cesRooms = roomsMapper.selectList(queryWrapper);
        if ("0".equals(type) || "1".equals(type)) {
            cesRooms.forEach(e -> {
                e.setPassWord(firstValue + e.getPrefix() + e.getName() + endValue);
            });
        } else if ("2".equals(type)){
            cesRooms.forEach(e -> {
                e.setPassWord(firstValue + e.getName() + endValue);
            });
        } else if("3".equals(type)){
            AtomicInteger num = new AtomicInteger();
            try {
                num.set(Integer.parseInt(type));
            } catch (NumberFormatException e){
                throw new JeecgBootException("参数错误");
            }
            cesRooms.forEach(e -> {
                num.getAndIncrement();
                e.setPassWord(Integer.toString(num.get()));
            });
        }
        return updateBatchById(cesRooms);
    }



    /**
     * 创建房间
     *
     * @param roomsDto
     * @return
     */
    public Result create(CesRoomsDto roomsDto) {
        roomsDto.setCreateAt(LocalDateTime.now());
        roomsDto.setSort(99);
        roomsDto.setInvalid(false);
        roomsDto.setUpdateAt(LocalDateTime.now());
        roomsMapper.insert(roomsDto);
        return Result.ok("创建成功!");
    }


    public Result modify(CesRoomsDto roomsDto) {
        CesRooms room = baseMapper.selectOne(Wrappers.<CesRooms>lambdaQuery().eq(CesRooms::getId, roomsDto.getId()).eq(CesRooms::getInvalid, false));
        if (ObjectUtils.isEmpty(room)) return Result.ok("数据未找到!");
        BeanUtil.copyProperties(roomsDto, room, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        room.setUpdateAt(LocalDateTime.now());
        roomsMapper.updateById(room);
        return Result.ok("修改成功!");
    }

    /**
     * 前端删除一个
     *
     * @param id
     * @return
     */
    public Result deleteOne(String id) {
        CesRooms rooms = roomsMapper.selectOne(Wrappers.<CesRooms>lambdaQuery().eq(CesRooms::getId, id).eq(CesRooms::getInvalid, false));
        if (ObjectUtils.isEmpty(rooms)) return Result.ok("删除成功!");
        CesRooms cesRoom = getById(id);
        if (StrUtil.isNotEmpty(cesRoom.getLivingOrderId())){
            return Result.error("房间下有居住订单，不能删除");
        }
        rooms.setInvalid(true);
        rooms.setUpdateAt(LocalDateTime.now());
        roomsMapper.updateById(rooms);
        return Result.ok("删除成功!");
    }

    public Result delete(List<String> ids) {
        List<CesRooms> rooms = roomsMapper.selectBatchIds(ids);
        if (CollectionUtil.isEmpty(rooms)) return Result.ok("删除成功!");
        rooms.forEach(v -> {
            v.setUpdateAt(LocalDateTime.now());
            v.setInvalid(true);
        });
        saveOrUpdateBatch(rooms);
        return Result.ok("删除成功!");
    }


    public Result removeAll(String hotelId) {
        List<CesRooms> rooms = roomsMapper.selectList(Wrappers.<CesRooms>lambdaQuery().eq(CesRooms::getHotelId, hotelId).eq(CesRooms::getInvalid, false));
        if (CollectionUtil.isEmpty(rooms)) return Result.ok("删除成功!");
        rooms.forEach(v -> {
            v.setUpdateAt(LocalDateTime.now());
            v.setInvalid(true);
        });
        saveOrUpdateBatch(rooms);
        return Result.ok("全部删除成功!");
    }


    /**
     * 批量新增楼层房间
     *
     * @param batchRoomsDto
     * @return
     */
    @Transactional(rollbackOn = Exception.class)
    public Result saveBatchRoom(BatchRoomsDto batchRoomsDto) {
        CesRoomBuildingFloor buildingFloor = buildingFloorService.getBuildFloorById(batchRoomsDto.getBuildId());
        if (ObjectUtils.isEmpty(buildingFloor)) return Result.error("楼栋不正确,请重新选择!");
        List<BatchBuildFloorRoomDto> floorRoomsList = batchRoomsDto.getChildren();
        List<CesRooms> rooms = new ArrayList<>();
        floorRoomsList.forEach(v -> {
            //生成楼层数据
            CesRoomBuildingFloor floor = new CesRoomBuildingFloor();
            floor.setHotelId(batchRoomsDto.getHotelId());
            floor.setParentId(buildingFloor.getId());
            floor.setName(v.getFloorName());
            floor.setType(CouponEnums.RoomBuildingFloorTypeEnum.FLOOR.code());
            floor.setCreateAt(LocalDateTime.now());
            floor.setUpdateAt(LocalDateTime.now());

            buildingFloorService.save(floor);
            List<CesRooms> roomsList = v.getChildren();
            roomsList.forEach(room -> {
                room.setHotelId(batchRoomsDto.getHotelId());
                room.setBuildId(buildingFloor.getId());
                room.setFloorId(floor.getId());
                room.setCreateAt(LocalDateTime.now());
                room.setUpdateAt(LocalDateTime.now());
                rooms.add(room);
            });
        });
        saveBatch(rooms);
        return Result.ok("保存成功!");
    }


    /**
     * 根据楼层获取房间数据
     *
     * @param floorId
     * @return
     */
    public Result queryByFloorId(String floorId) {
        List<CesRooms> rooms = roomsMapper.selectList(Wrappers.<CesRooms>lambdaQuery().eq(CesRooms::getFloorId, floorId).eq(CesRooms::getInvalid, false));
        return Result.OK(rooms);
    }

    /**
     * 根据hotelId获取房间数据
     *
     * @param hotelId
     * @return
     */
    public Result queryByHotelId(String hotelId) {
        List<CesRooms> rooms = roomsMapper.selectList(Wrappers.<CesRooms>lambdaQuery().eq(CesRooms::getHotelId, hotelId).eq(CesRooms::getInvalid, false));
        return Result.OK(rooms);
    }

    public List<CesRooms> getByHotelId(String hotelId) {
        List<CesRooms> rooms = roomsMapper.selectList(Wrappers.<CesRooms>lambdaQuery().eq(CesRooms::getHotelId, hotelId).eq(CesRooms::getInvalid, false));
        return rooms;
    }

    /**
     * 获取能使用的房间
     *
     * @param param
     * @return
     */
    public List<CanUseResultVo> getCanUseRooms(CanUseRequestParamDto param) {
        // 获取房型
        QueryWrapper<CesRoomLayout> wrappers = Wrappers.<CesRoomLayout>query().eq("hotel_id", param.getHotelId())
                .eq("invalid", false);
        if ((param.getCancelLayout() == null || !param.getCancelLayout()) && param.getLayoutId() != null && !param.getLayoutId().isEmpty()) {
            wrappers.eq("id", param.getLayoutId());
        }
        List<CesRoomLayout> layouts = layoutService.list(wrappers);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");

        List<CesRooms> allRooms = baseMapper.canUseRooms(param.getHotelId(), formatter.format(param.getStartOf()), formatter.format(param.getEndOf()));
        // 预定未排房订单
        //        List<AlreadyUseLayoutNumberVo> useLayoutNumbers = baseMapper.alreadyUseLayoutNumber(param.getHotelId(), param.getStartOf(), param.getEndOf());
        List<CesRoomBuildingFloor> allBuildingFloors = buildingFloorService.list(Wrappers.<CesRoomBuildingFloor>query().eq("hotelId", param.getHotelId()).eq("invalid", false));
        List<CanUseResultVo> results = new ArrayList<>();
        List<CesRoomBuildingFloor> floors = allBuildingFloors.stream().filter(s -> !s.getParentId().equals("0")).collect(Collectors.toList());

        BigDecimal hourPrice = null;
        List<String> relationHourRoomLayoutIds = new ArrayList<>();
        if (param.getBookingType().equals(CheckInTypeEnum.HOUR_TIME.getKey())) {
            if (param.getHourRoomRuleId() == null)
                throw new JeecgBootException("参数错误");
            CesHourRoomRule hourRoomRule = hourRoomRuleService.getById(param.getHourRoomRuleId());
            if (hourRoomRule == null) throw new JeecgBootException("钟点房计费规则未找到");
            if (hourRoomRule.getLayoutIds() != null && !hourRoomRule.getLayoutIds().isEmpty()) {
                relationHourRoomLayoutIds.addAll(Arrays.stream(hourRoomRule.getLayoutIds().split(",")).collect(Collectors.toList()));
            }

            hourPrice = hourRoomRule.getAfterOpenRoomPay();
        }
        BigDecimal finalHourPrice = hourPrice;
        List<CesRooms> choiceRooms = new ArrayList<>();
        if (param.getOrderId() != null && !param.getOrderId().isEmpty()) {
            List<BusBookingRooms> bookingRooms = bookingRoomsService.list(Wrappers.<BusBookingRooms>query().eq("booking_orders_id", param.getOrderId()));
            List<String> choiceRoomIds = bookingRooms.stream().map(w -> w.getRoomId()).collect(Collectors.toList());
            choiceRooms = list(Wrappers.<CesRooms>query().in("id", choiceRoomIds));
        }
        List<CesRooms> finalChoiceRooms = choiceRooms;
        if (param.getCancelLayout() != null && param.getCancelLayout()) {
            CanUseResultVo item = new CanUseResultVo();
            final Integer[] canUseRoomCount = {0};
            List<CanUseBuildingRoomsVo> floorVos = new ArrayList<>();
            floors.forEach(c -> {
                CanUseBuildingRoomsVo floorRoomsVo = new CanUseBuildingRoomsVo();
                List<CesRooms> floorRooms = allRooms.stream().filter(e -> e.getFloorId().equals(c.getId())).collect(Collectors.toList());
                Optional<CesRoomBuildingFloor> opBuilding = allBuildingFloors.stream().filter(q -> q.getId().equals(c.getParentId())).findFirst();
                if (!opBuilding.isPresent()) return;

                floorRoomsVo.setFloorName(c.getName());
                floorRoomsVo.setFloorId(c.getId());
                floorRoomsVo.setBuildingName(opBuilding.get().getName());
                floorRoomsVo.setBuildingId(opBuilding.get().getId());
                canUseRoomCount[0] += floorRooms.size();
                // 设置完size之后添加已用房间 不影响数量
                List<String> currentFloorRoomIds = floorRooms.stream().map(r -> r.getId()).collect(Collectors.toList());
                List<CesRooms> orderRooms = finalChoiceRooms.stream().filter(y -> y.getFloorId().equals(c.getId()) && !currentFloorRoomIds.contains(y.getId())).collect(Collectors.toList());
                floorRooms.forEach(p -> {
                    Optional<CesRoomLayout> opLayout = layouts.stream().filter(i -> i.getId().equals(p.getLayoutId())).findFirst();
                    if (opLayout.isPresent()) {
                        CesRoomLayout roomLayout = opLayout.get();
                        p.setMarketPrice(roomLayout.getMarketPrice());
                    }
                });
                floorRooms.addAll(orderRooms);
                floorRoomsVo.setFloorRooms(floorRooms);
                floorVos.add(floorRoomsVo);
            });
            item.setLayout(null);
            item.setBuildingRooms(floorVos);
            results.add(item);
            return results;
        }
        layouts.forEach(s -> {
            CanUseResultVo item = new CanUseResultVo();
            List<CanUseBuildingRoomsVo> floorVos = new ArrayList<>();
            final Integer[] canUseRoomCount = {0};
            // 钟点房和全天房计费方案取到对应的价格
            if (param.getBookingType().equals(CheckInTypeEnum.ALL_DAYS.getKey())) {
                s.setFavPrice(s.getMarketPrice());
            } else if (param.getBookingType().equals(CheckInTypeEnum.HOUR_TIME.getKey())) {
                // 判定是否支持房价方案
                s.setIsSupportHourRule(relationHourRoomLayoutIds.stream().anyMatch(p -> p.equals(s.getId())));
                s.setFavPrice(finalHourPrice);
            } else
                s.setFavPrice(null);

            floors.forEach(c -> {
                CanUseBuildingRoomsVo floorRoomsVo = new CanUseBuildingRoomsVo();
                List<CesRooms> floorRooms = allRooms.stream().filter(e -> e.getFloorId().equals(c.getId()) && e.getLayoutId().equals(s.getId())).collect(Collectors.toList());
                Optional<CesRoomBuildingFloor> opBuilding = allBuildingFloors.stream().filter(q -> q.getId().equals(c.getParentId())).findFirst();
                if (!opBuilding.isPresent()) return;

                floorRoomsVo.setFloorName(c.getName());
                floorRoomsVo.setFloorId(c.getId());
                floorRoomsVo.setBuildingName(opBuilding.get().getName());
                floorRoomsVo.setBuildingId(opBuilding.get().getId());
                canUseRoomCount[0] += floorRooms.size();
                // 设置完size之后添加已用房间 不影响数量
                List<String> currentFloorRoomIds = floorRooms.stream().map(r -> r.getId()).collect(Collectors.toList());
                List<CesRooms> orderRooms = finalChoiceRooms.stream().filter(y -> y.getFloorId().equals(c.getId()) && !currentFloorRoomIds.contains(y.getId()) && y.getLayoutId().equals(s.getId())).collect(Collectors.toList());
                floorRooms.addAll(orderRooms);
                floorRoomsVo.setFloorRooms(floorRooms);
                floorVos.add(floorRoomsVo);
            });
            // 主要为了排除掉未排房数量
//            Optional<AlreadyUseLayoutNumberVo> alreadyUseLayoutNumberVo = useLayoutNumbers.stream().filter(k -> k.getLayoutId().equals(s.getId())).findAny();
//            if (alreadyUseLayoutNumberVo.isPresent()) {
//                canUseRoomCount[0] = canUseRoomCount[0] - alreadyUseLayoutNumberVo.get().getUseCount();
//                // 算法如果正确 就不需要下面这步，待检验之前步骤正确性
//                if (canUseRoomCount[0] < 0) canUseRoomCount[0] = 0;
//            }
            s.setCanUseCount(canUseRoomCount[0]); //设置可用房间数
            item.setLayout(s);
            item.setBuildingRooms(floorVos);
            results.add(item);
        });
        return results;

    }

    /**
     * 在住来源分组统计
     *
     * @param hotelId
     * @return
     */
    public List<LivingSourceStatVo> livingSourceStat(String hotelId) {
        return roomsMapper.livingSourceStat(hotelId);
    }

    /**
     * 房态统计
     * @param hotelId
     * @return
     */
    public List<LivingSourceStatVo> roomLiveStat(String hotelId){
        return roomsMapper.roomLiveStat(hotelId);
    }

    // 根据房间id获取对应房型的门市价
    public BigDecimal getRoomMktPrice(String roomId) {
        if(roomId == null || roomId.isEmpty()) throw new JeecgBootException("参数错误");
        CesRooms room = getById(roomId);
        if(room == null) throw new JeecgBootException("房间不存在");
        CesRoomLayout layout = layoutService.getById(room.getLayoutId());
        if(layout == null) throw new JeecgBootException("房型不存在");
        return  layout.getMarketPrice();

    }

    // 如果是空间，且为今天预约，将房间状态改为预约态
    public Boolean getReserveRoom() {
        // 将Calendar设置为当前日期
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        String nowDateStr = dateFormat.format(calendar.getTime());
        calendar.add(Calendar.DAY_OF_YEAR, 1);
        String tomorrowStr = dateFormat.format(calendar.getTime());

        // 将日期增加1天
        calendar.add(Calendar.DAY_OF_YEAR, 1);
        List<CesRooms> CesRoomList = roomsMapper.getReserveRoom(nowDateStr, tomorrowStr);
        ArrayList<CesRooms> newCesRooms = new ArrayList<>();
        for (CesRooms cesRoom : CesRoomList){
            if (cesRoom.getRoomStatus() < 3){
                cesRoom.setLastRoomStatus(cesRoom.getRoomStatus());
                cesRoom.setRoomStatus(RoomStatusEnum.RESERVE.getKey());
                newCesRooms.add(cesRoom);
            }
        }
        return updateBatchById(newCesRooms);
    }

    public Boolean returnReserveRoom() {
        List<CesRooms> roomList = list(Wrappers.<CesRooms>lambdaQuery().eq(CesRooms::getRoomStatus, RoomStatusEnum.RESERVE.getKey()));
        List<String> ids = roomList.stream().map(CesRooms::getId).collect(Collectors.toList());
        if (ids.isEmpty()){
            return true;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_YEAR, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        List<BusRoomBookingOrders> bookingOrderList = roomBookingOrdersMapper.findBookingOrder(ids, new Date(), calendar.getTime());
        List<String> orderIds = bookingOrderList.stream().map(BusRoomBookingOrders::getId).collect(Collectors.toList());
        Set<String> bookingRoomIds;
        if (!orderIds.isEmpty()){
            List<BusBookingRooms> bookingRoomsList = bookingRoomsService.list(Wrappers.<BusBookingRooms>lambdaQuery().in(BusBookingRooms::getBookingOrdersId, orderIds));
            bookingRoomIds = bookingRoomsList.stream().map(BusBookingRooms::getRoomId).collect(Collectors.toSet());
        } else {
            bookingRoomIds = new HashSet<>();
        }
        List<String> idList = ids.stream().filter(e -> !bookingRoomIds.contains(e)).collect(Collectors.toList());
        UpdateWrapper<CesRooms> updateWrapper = new UpdateWrapper<>();
        if (idList.isEmpty()){
            return true;
        }
        updateWrapper.setSql("room_status = last_room_status").in("id",idList);
        return update(updateWrapper);
    }

    public Integer changeLivingRoomStatus(Integer status, Integer lastStatus) {
        if (RoomStatusEnum.EMPTY_CLEAR.getKey().equals(status)) {
            return RoomStatusEnum.LIVE_CLEAR.getKey();
        } else if (RoomStatusEnum.EMPTY_DIRTY.getKey().equals(status)){
            return RoomStatusEnum.LIVE_DIRTY.getKey();
        } else if (RoomStatusEnum.RESERVE.getKey().equals(status)) {
            return changeLivingRoomStatus(lastStatus, RoomStatusEnum.EMPTY_DIRTY.getKey());
        } else {
            throw new JeecgBootException("当前状态不可入住");
        }
    }
}
